# -*- coding:utf-8 -*-

from queue import Queue
from threading import Lock, Thread
import time
import requests
from lxml import etree

requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告

"""
多线程队列
"""

class myThread(Thread):
    def __init__(self, threadID, name, threadLock, q):
        Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.threadLock = threadLock
        self.q = q

    def run(self):
        print("开启线程：" + self.name)
        process_data(self.name, self.threadLock, self.q)
        print("退出线程：" + self.name)


def process_data(threadName, threadLock, q):
    while not EXIT_FLAG:
        # threadLock.acquire()
        if not workQueue.empty():
            data = q.get()
            # threadLock.release()
            print("%s: processing %s" % (threadName, data))
            headers = {
                'authority': 'www.anpel.com.cn',
                'method': 'GET',
                'path': '/products_1120019.html',
                'scheme': 'https',
                'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
                'accept-encoding': 'gzip, deflate, br',
                'accept-language': 'zh-CN,zh;q=0.9',
                'cache-control': 'max-age=0',
                'cookie': '_ga=GA1.3.577542334.1605067253; _gid=GA1.3.672374784.1605067253; Hm_lvt_47b9841342068855c7053b718fe9f289=1605067254; Hm_lvt_aa507d031177fe20a161fee3282aeeb4=1605067254; ASP.NET_SessionId=mjdgg2vxgf0r43qksmxiyde2; Hm_lpvt_47b9841342068855c7053b718fe9f289=1605081444; Hm_lpvt_aa507d031177fe20a161fee3282aeeb4=1605081444',
                'dnt': '1',
                'sec-ch-ua': '"Chromium";v="86", "\"Not\\A;Brand";v="99", "Google Chrome";v="86"',
                'sec-ch-ua-mobile': '?0',
                'sec-fetch-dest': 'document',
                'sec-fetch-mode': 'navigate',
                'sec-fetch-site': 'none',
                'sec-fetch-user': '?1',
                'upgrade-insecure-requests': '1',
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
            }

            res = requests.get(data, headers=headers, verify=False)
            selector = etree.HTML(res.text)
            aa = "".join(selector.xpath('//span[@id="lblProductName"]/text()')).replace("标准品", "").strip()  # 规格
            print(aa)
        else:
            # threadLock.release()
            pass
        time.sleep(1)

if __name__ == "__main__":
    EXIT_FLAG = 0
    threadNum = 10
    threadList = []

    for t in range(1, threadNum + 1):
        threadList.append("Thread-" + str(t))

    # nameList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
    nameList = [
        'https://www.anpel.com.cn/products/1129895.html',
        'https://www.anpel.com.cn/products_1439341.html',
        'https://www.anpel.com.cn/products_971157.html',
        'https://www.anpel.com.cn/products/971756.html',
        'https://www.anpel.com.cn/products_6003.html',
        'https://www.anpel.com.cn/products_1354243.html',
        'https://www.anpel.com.cn/products/971703.html',
        'https://www.anpel.com.cn/products_1398391.html',
        'https://www.anpel.com.cn/products/6017.html',
        'https://www.anpel.com.cn/products/6011.html',
        'https://www.anpel.com.cn/products_6044.html',
        'https://www.anpel.com.cn/products_1426242.html',
        'https://www.anpel.com.cn/products/49209.html',
        'https://www.anpel.com.cn/products/6045.html',
        'https://www.anpel.com.cn/products/49210.html',
        'https://www.anpel.com.cn/products_6043.html',
        'https://www.anpel.com.cn/products/6035.html',
        'https://www.anpel.com.cn/products/6034.html',
        'https://www.anpel.com.cn/products/6005.html',
        'https://www.anpel.com.cn/products/48598.html',
        'https://www.anpel.com.cn/products/6032.html',
        'https://www.anpel.com.cn/products/1348782.html',
        'https://www.anpel.com.cn/products/5405.html',
        'https://www.anpel.com.cn/products_1439340.html',
        'https://www.anpel.com.cn/products/48638.html',
        'https://www.anpel.com.cn/products/971774.html',
        'https://www.anpel.com.cn/products/6022.htm'
    ]
    threadLock = Lock()
    workQueue = Queue(28)
    threads = []
    threadID = 1

    # 创建新线程
    for tName in threadList:
        thread = myThread(threadID, tName, threadLock, workQueue)
        thread.start()
        threads.append(thread)
        threadID += 1

    # 填充队列
    # threadLock.acquire()
    for word in nameList:
        workQueue.put(word)
    # threadLock.release()

    # 等待队列清空
    while not workQueue.empty():
        pass

    # 通知线程是时候退出
    EXIT_FLAG = 1

    # 等待所有线程完成
    for t in threads:
        t.join()
    print("退出主线程")
